package com.cuckoo.cuckoo_app2.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.dao.GenerationListItem;

import java.util.List;


public class GenerationListAdapter extends RecyclerView.Adapter<GenerationListAdapter.GenerationListViewHolder> {

    private List<GenerationListItem> dataList;
    private Context context;

    public GenerationListAdapter(Context context,List<GenerationListItem> dataList) {
        this.dataList = dataList;
        this.context = context;
    }

    @NonNull
    @Override
    public GenerationListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_userlist_compon, parent, false);
        return new GenerationListAdapter.GenerationListViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull GenerationListViewHolder holder, int position) {
        GenerationListItem item = dataList.get(position);
        holder.item_userlist_name.setText(item.getAccount());
        holder.item_userlist_time.setText(item.getTime());
        if (item.getIsVip().equals("1")) {
            holder.item_userlist_isVip.setVisibility(View.VISIBLE);
        } else {
            holder.item_userlist_isVip.setVisibility(View.GONE);
        }
    }

    // 清空数据并通知更新
    public void clearData() {
        this.dataList.clear(); // 假设 mData 不是 null
        notifyDataSetChanged();
    }

    // 更新数据列表的方法
    public void updateData(List<GenerationListItem> dataList) {
        this.dataList = dataList; // 直接替换数据列表
        notifyDataSetChanged(); // 通知RecyclerView数据已更改
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class GenerationListViewHolder extends RecyclerView.ViewHolder {
        TextView item_userlist_name;
        TextView item_userlist_time;
        ImageView item_userlist_isVip;

        public GenerationListViewHolder(@NonNull View itemView) {
            super(itemView);
            item_userlist_name = itemView.findViewById(R.id.item_userlist_name);
            item_userlist_time = itemView.findViewById(R.id.item_userlist_time);
            item_userlist_isVip = itemView.findViewById(R.id.item_userlist_isVip);
        }
    }

}